<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        placeholder="请输入角色名称"
        v-model="listQuery.role_name"
        style="width: 200px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />

      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查询</el-button
      >
    </div>
    <div class="options-container">
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="addRol()"
        >添加</el-button
      >
    </div>
    <div class="table-container">
      <el-table
        v-loading="listLoading"
        :key="tableKey"
        :data="list"
        border
        fit
        @sort-change="sortHanlde"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          prop="id"
          sortable="custom"
          align="left"
          width="105"
        >
        </el-table-column>
        <el-table-column
          label="角色"
          prop="role_name"
          sortable="custom"
          width="150px"
          align="left"
        >
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="create_time"
          sortable="custom"
          width="160px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="300"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="authModule(scope.row)"
              >授权</el-button
            >
            <el-button type="success" size="mini" @click="updateRol(scope.row)"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="removeRole(scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="pageHandle"
      />
    </div>
    <div class="dialog-container">
      <!-- 角色编辑弹框 -->
      <rolesEdit
        :show.sync="rolesEdit.show"
        :data="rolesEdit.data"
        @success="rolesEditSuccess"
      >
      </rolesEdit>

      <rolesAdd :show.sync="rolesAdd.show" @success="rolesEditSuccess">
      </rolesAdd>

      <rolesAuthCtl
        :show.sync="rolesAuth.show"
        :role_id="rolesAuth.role_id"
        :role_name="rolesAuth.role_name"   
      >
      </rolesAuthCtl>
    </div>
  </div>
</template>

<script>
import { getlist, remove_by_id } from "@/api/right_roles";
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import rolesAdd from "./components/rolesAdd";
import rolesEdit from "./components/rolesEdit";
import rolesAuthCtl from "./components/rolesAuth";

export default {
  name: "rolesManage",
  components: { Pagination, rolesAdd, rolesEdit, rolesAuthCtl },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        role_name: "",
        page: 1,
        limit: 20,
        filed: "id",
        sort: "ascending",
      },
      rolesEdit: {
        show: false,
        data: undefined,
      },
      rolesAdd: {
        show: false,
        data: undefined,
      },
      rolesAuth: {
        show: false,
        role_id: 0,
        role_name: "",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      getlist(this.listQuery).then((response) => {
        this.list = response.data;
        this.total = response.count || 0;
        setTimeout(() => {
          this.listLoading = false;
        }, 500);
      });
    },
    handleFilter() {
      this.getList();
    },
    sortHanlde(column) {
      console.log(column);
      this.listQuery.filed = column.prop;
      this.listQuery.sort = column.order;
      this.getList();
    },
    pageHandle(page) {
      this.listQuery.page = page.page;
      this.listQuery.limit = page.limit;
      this.getList();
    },
    removeRole(row) {
      this.$confirm(
        `此操作将永久删除【${row.role_name}】角色, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          remove_by_id(row.id).then((response) => {
            if (response.data > 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            } else {
              this.$message({
                type: "info",
                message: "删除失败",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    updateRol(row) {
      this.rolesEdit.show = true;
      this.rolesEdit.data = row;
    },
    rolesEditSuccess() {
      this.getList();
    },
    addRol(row) {
      this.rolesAdd.show = true;
    },
    authModule(row) {
      this.rolesAuth.show = true;
      this.rolesAuth.role_id = row.id;
      this.rolesAuth.role_name = row.role_name;
    },
  },
};
</script>
